$(function(){


    /*选择磅数*/
    $("ul#all_Size li p").click(function(){

        $(this).addClass("");
        G.goods.sizeChange($(this).attr('id').replace('size_id_', ''), 1);
    });


    //加入购物车
    $('#add_cart').click(function() {
        var goods_amount = E.trim($("#buy_number").val());
        if (!E.isDigital(goods_amount)) {
            goods_amount = 1;
        }
        G.goods.add($("#pro_id").val(), goods_amount);
    });

    //手动输入商品数量控制
    $(document).on('keyup', '#buy_number', function(){
        var value = E.trim($(this).val());
        if(!E.isInt(value) || value < 1)
            value = 1;

        if(value > 49){
            value = 49;
            $(".p_right").css("opacity",".5");
        }else{
            $(".p_right").css("opacity","1");
        }

        $(this).val(value);
    });


    //立即购买
    $(document).on('click','#buy_now',function() {
        var goods_amount = E.trim($("#buy_number").val());
        if (!E.isDigital(goods_amount)) {
            goods_amount = 1;
        }
        G.cart.now_buy($("#pro_id").val(), goods_amount);
    });

    /*更多促销信息 12.24 mark*/
    $(document).on("click","#more_click",function(){
        $("#more_show,#more_close").show(200);
        $(this).hide(200);
    });
    /*收起更多 12.24 mark*/
    $(document).on("click","#more_close",function(){
        $('#more_show').hide(200);
        $(this).hide(200);
        $("#more_click").show();
    });


});


G.goods.init = function() {
    E.ajax_get({
        action: "goods",
        operFlg: 2,
        data: {
            id: this.postID
        },
        call: "G.goods.display"
    });
}

G.goods.display = function( o ) {

    if (o.code == 200) {

        if (o.data.goodsList.length == 1) {

            $("body").append("<input type='hidden' value='0' id='size_id_" + this.postID + "' />");
            $("#size_id_" + this.postID).data(o.data.goodsList[0]);
            this.sizeChange(this.postID, 0);

        } else {

            var id = "";
            $.each(o.data.goodsList, function(k, v) {
                if (v.useFlg == 1 || v.useFlg == 2) {
                    if (id == "" || v.postID == G.goods.postID)
                        id = v.postID;
                    if(v.postID == G.goods.postID && v.useFlg == 2){
                        $("#buy_now").removeClass('red_btn').val('已售罄');
                        $(".p_left,.p_right").addClass('nopoint').css('opacity','0.5');
                        $("#buy_number").val(0).attr('disabled','disabled');
                        $("#buy_now,#add_cart").addClass('disable').attr('disabled','disabled');
                    }
                    $("#size_id_" + v.postID).data(v);
                } else {
                    $("#size_id_" + v.postID).addClass("stockout");
                }
            });
            if (id != "") {
                this.sizeChange(id, 1);
            } else {
                $("#addCart").text("当前商品已下架或库存不足，请选择其他商品").addClass("empty");
            }
        }
        $('#comments_count').html('累计点评(' + o.data.comment_count + ') <small>Evaluation</small>');
    }
}

G.goods.init();

G.goods.sizeChange = function( id, flg ) {

    $("#size_id_" + id).addClass("cur");
    $("#size_id_" + id).parent().siblings().find('p').removeClass("cur");

    var obj = $("#size_id_" + id);
    var dt = obj.data();

    if(dt.useFlg == 2){
        $("#buy_now").removeClass('red_btn').val('已售罄');
        $(".p_left,.p_right").addClass('nopoint').css('opacity','0.5');
        $("#buy_number").val(0).attr('disabled','disabled');
        $("#buy_now,#add_cart").addClass('disable').attr('disabled','disabled');
    }else{
        $("#buy_now").addClass('red_btn').val('立即订购');
        $(".p_left").removeClass('nopoint').css('opacity','0.5');
        $(".p_right").removeClass('nopoint').css('opacity','1');
        $("#buy_number").val(1).removeAttr('disabled');
        $("#buy_now,#add_cart").removeClass('disable').removeAttr('disabled');
    }

    $("#price").text(parseFloat(dt.price).toFixed(2));
    $("#pro_id").val(dt.postID);
    $('#g_edible').text(obj.attr('edible'));
    $('#g_size').text(obj.attr('size'));
    $('#g_aheadTime').text(obj.attr('aheadTime'));

}

//添加商品
G.goods.add = function( postID, goods_amount ) {
    E.loadding.open("正在添加商品到购物车，请稍候...");
    E.ajax_post({
        action: "cart",
        operFlg: 2,
        data: {
            postID: postID,
            goods_amount: goods_amount
        },
        call: function( o ) {
            if (o.code == 200) {
                G.cart.comm.get_amount("cart_amount");
                self.location = G.args.cart
            } else {
                E.loadding.close();
                E.alert(o.message);
            }
        }
    });
}

G.goods.comment = function( page ) {
    E.ajax_get({
        action: 'goodsComment',
        operFlg: 1,
        data: {
            twoPostID: this.twoPostID,
            page: page,
            subsite: 'wap'
        },
        call: function( o ) {
            if (o.code == 200) {
                var html = '';
                $.each(o.data.comment, function(k, v) {
                    html += '<li>';
                    html += '<p>' + v.comment_content + '</p>';
                    html += '<span> ' + v.creator + '<br><small>' + v.createTime + '</small></span>';
                    html += '</li>';
                });
                $('#comment_list').html(html);
                $('#paging').html(o.data.paging);
            } else {
                $('.Review_top').hide();
                $('#comment_list').html('<li>暂无点评</li>');
                $('#paging').hide();
            }
        }
    });
}

G.goods.comment(1);